package com.gem.sms.web;

import java.io.IOException;
import java.time.LocalDate;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gem.sms.entity.Student;
import com.gem.sms.service.StudentService;
import com.gem.sms.util.BeanFactory;
import com.gem.sms.util.PageBean;

@WebServlet(name = "StuInfoServlet", urlPatterns = "/permission/stuInfo/*", loadOnStartup = 2)
public class StuInfoServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	StudentService ss = (StudentService) BeanFactory.getBean("studentService");

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String base = req.getContextPath();
		String pathInfo = req.getPathInfo();
		String nextPage = "";
		boolean isRedirect = false;
		if ("/index".equals(pathInfo)) {
			nextPage = "list";
		} else if ("/list".equals(pathInfo)) {
			String pageNowStr = req.getParameter("pageNow");
			String searchName = req.getParameter("searchName");
			if (pageNowStr == null) {
				//说明是第一次登陆
				pageNowStr = "1";
			}
			Integer pageNow = Integer.parseInt(pageNowStr);
			//分页查询
			PageBean<Student> pageBean = ss.ListStudentsByPages(pageNow, 3, searchName);
			req.setAttribute("pageBean", pageBean);
			nextPage = "datas";
		} else if ("/checkInfo".equals(pathInfo)) {
			String stuno = req.getParameter("stuno");
			Student student = ss.getStudentById(stuno);
			req.setAttribute("sinfo", student);
			nextPage = "stuInfo";
		} else if ("/preEdit".equals(pathInfo)) {
			String stuno = req.getParameter("stuno");
			Student student = ss.getStudentById(stuno);
			req.setAttribute("einfo", student);
			nextPage = "edit";
		} else if ("/edit".equals(pathInfo)) {
			String name = req.getParameter("name");
			String sno = req.getParameter("stuNo");
			String password = req.getParameter("password");
			String datestr = req.getParameter("birthday");
			Integer sex = Integer.parseInt(req.getParameter("sex"));
			Integer polity = Integer.parseInt(req.getParameter("polity"));
			String brief = req.getParameter("brief");
      		System.out.println(ss.getStudentById(sno));
			Integer id = ss.getStudentById(sno).getId();
			Student stu1 = new Student(id, name, sno, password, LocalDate.parse(datestr), sex, polity, brief);
			ss.updateStu(stu1);
			//nextPage = "/permission/stuInfo/list";
			nextPage = "/permission/stuInfo/index";
			isRedirect = true;
		} else if ("/delete".equals(pathInfo)) {
			String stuno = req.getParameter("stuno");
			ss.delete(stuno);
			//nextPage = "/permission/stuInfo/list";
			nextPage = "/permission/stuInfo/index";
			isRedirect = true;
		} else if ("/preAdd".equals(pathInfo)) {
			nextPage = "register";
		} else if ("/add".equals(pathInfo)) {
			String name = req.getParameter("name");
			String sno = req.getParameter("stuNo");
			String password = req.getParameter("password");
			String datestr = req.getParameter("birthday");
			Integer sex = Integer.parseInt(req.getParameter("sex"));
			Integer polity = Integer.parseInt(req.getParameter("polity"));
			String brief = req.getParameter("brief");
			Student stu1 = new Student(name, sno, password, LocalDate.parse(datestr), sex, polity, brief);
			ss.addStu(stu1);
			//nextPage = "/permission/stuInfo/list";
			nextPage = "/permission/stuInfo/index";
			isRedirect = true;
		}

		if (isRedirect) {
			resp.sendRedirect(base + nextPage);
		} else {
			req.getRequestDispatcher("/WEB-INF/pages/" + nextPage + ".jsp").forward(req, resp);
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}
